﻿#include "CSparePart.h"
#include <string.h>
#include <stdlib.h>

int set_atom_data_shape(char* a, int shape) {
	*a &= 0xF0;
	*a |= shape;
	return 0;
}

CSparePart* spare_part_clone_of(const CSparePart* from) {
	CSparePart* ret = 0;
	if (!from) {
		return 0;
	}
	ret = (CSparePart*)malloc(sizeof(CSparePart));
	memcpy(ret, from, sizeof(CSparePart));
    return ret;
}

int spare_part_move_left(CSparePart* spare) {
	if (!spare) {
		return -1;
	}
	spare->origin.x--;
	return 0;
}

int spare_part_move_right(CSparePart* spare) {
	if (!spare) {
		return -1;
	}
	spare->origin.x++;
	return 0;
}


int spare_part_move_down(CSparePart* spare) {
	if (!spare) {
		return -1;
	}
	spare->origin.y++;
	return 0;
}

static int get_max_y(CSparePart* spare) {
	int y = -1;
	int i = 0;
	for (i = 0; i < spare->atom_cnt; ++i) {
		if (spare->atoms[i].coord.y > y) {
			y = spare->atoms[i].coord.y;
		}
	}
	return y;
}

//(x, y) --> (-y, x)  -->right move
int spare_part_rotate(CSparePart* spare) {
	int max_y = get_max_y(spare);
	int i = 0;

	for (i = 0; i < spare->atom_cnt; i++) {
		int old_x = spare->atoms[i].coord.x;
		spare->atoms[i].coord.x = max_y - spare->atoms[i].coord.y;
		spare->atoms[i].coord.y = old_x;
	}
	return 0;
}

